package data;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class ArpTemplate implements Serializable {
	private int steps;
	private String length;
	private int tune[];
	
	public ArpTemplate() {
		steps = 0;
		length = "";
		tune = new int[0];
	}

	public ArpTemplate(int steps, String length) {
		this.steps = steps;
		this.length = length;
		tune = new int[steps];
		for (int i = 0; i < steps; i++) tune[i] = 0;
		
	}
	
	public int getSteps() {
		return steps;
	}
	
	public String getLength() {
		return length;
	}
	
	public int[] getTune() {
		return tune;
	}
	
	public void setSteps(int steps) {
		if (this.steps < steps) {
			int[] tune = new int[steps];
			System.arraycopy(this.tune, 0, tune, 0, this.tune.length);
			for (int i = this.tune.length; i < tune.length; i++) {
				tune[i] = 1;
			}
			this.tune = tune;
		}
		this.steps = steps;
	}
	
	public void setTune(int tune[]) {
		this.tune = tune;
	}

	public void setLength(String length) {
		this.length = length;		
	}

	@Override
	public void serialize(OutputStream os) {
		try {
			os.write(steps);
			System.out.println("" + steps);
			for (int i = 0; i < steps; i++) {
				os.write(tune[i]);
				System.out.println("" + tune[i]);
			}
			os.write(Integer.parseInt(length.substring(length.indexOf("/") + 1, length.length())));
			System.out.println("" + Integer.parseInt(length.substring(length.indexOf("/") + 1, length.length())));
		} 
		catch (IOException ioe) {
			ioe.printStackTrace();
		}	
	}

	@Override
	public void deserialize(InputStream is) {
		try {
			if (is.available() > 0) {
				steps = is.read();
				tune = new int[steps];
				for (int i = 0; i < steps; i++) tune[i] = is.read();
				length = "1/" + is.read();
			}
		}
		catch (IOException ioe) {
			ioe.printStackTrace();
		}
	}
	
	@Override
	public String toString() {
		String res = "" + steps + " ";
		for (int i = 0; i < steps; i++) res += "" + tune[i] + " ";
		res += length;
		return res;
	}
}
